package com.serverdemo.application.scene;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.serverdemo.application.common.PacketIDConst;
import com.serverdemo.core.entity.SocketEntity;
import com.serverdemo.core.entity.SocketEntityStatusEnum;
import com.serverdemo.core.net.NetPacket;

/**
 * Created by wangqiang on 2020/4/10.
 */
public class Player extends Character
{
    private SocketEntity socketEntity;

    private String userName;

    private String data;

    private SceneProcessor sceneProcessor;

    public void setSceneProcessor(SceneProcessor sceneProcessor)
    {
        this.sceneProcessor = sceneProcessor;
    }

    public SocketEntity getSocketEntity()
    {
        return socketEntity;
    }

    public void setSocketEntity(SocketEntity socketEntity)
    {
        this.socketEntity = socketEntity;
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public String getData()
    {
        return data;
    }

    public void setData(String data)
    {
        this.data = data;
    }

    public SocketEntityStatusEnum getEntityStatus()
    {
        if (socketEntity == null)
        {
            return null;
        }

        return socketEntity.getStatusEnum();
    }

    public void processInput(int interval)
    {
        if (socketEntity == null)
        {
            return;
        }

        for (int i = 0; i < 12; i++)
        {
            NetPacket netPacket = socketEntity.popReceivedPacket();
            if (netPacket == null)
            {
                break;
            }

            dispatchPacket(netPacket);
        }
    }

    public void processOutput(int interval)
    {
        if (socketEntity == null)
        {
            return;
        }

        for (int i = 0; i < 100; i++)
        {
            NetPacket netPacket = socketEntity.popSendPacket();
            if (netPacket == null)
            {
                return;
            }

            socketEntity.getChannel().writeAndFlush(netPacket);
        }
    }

    public void dispatchPacket(NetPacket netPacket)
    {
        int packetId = netPacket.getId();
        byte[] body = netPacket.getBody();
        if (body == null)
        {
            return;
        }

        switch (packetId)
        {
            case PacketIDConst
                    .CS_GET_SCENE_PLAYER_COUNT:
            {
                CSGetScenePlayerCount packet = JSON.parseObject(body, CSGetScenePlayerCount.class);
                handleGetScenePlayerCount(packet);
                break;
            }
        }
    }

    public void handleGetScenePlayerCount(CSGetScenePlayerCount packet)
    {
        SCGetScenePlayerCount response = new SCGetScenePlayerCount();
        response.setPlayerCount(sceneProcessor.getPlayerCount());
        sendNetPacket(PacketIDConst.SC_GET_SCENE_PLAYER_COUNT, response);
    }

    public void sendNetPacket(int packetID, Object packet)
    {
        if (socketEntity == null)
        {
            return;
        }

        byte[] bytes = JSONObject.toJSONBytes(packet);
        NetPacket netPacket = new NetPacket();
        netPacket.setId(packetID);
        netPacket.setBody(bytes);
        netPacket.setLength(bytes.length);

        socketEntity.pushSendPacket(netPacket);
    }

}
